package com.gxa.saas.portal.controller.btopfore;


import com.gxa.saas.core.utils.JwtUtil;
import com.gxa.saas.portal.entity.vo.wylvo.GoodsVo;
import com.gxa.saas.portal.entity.vo.wylvo.OrderInfoVO;
import com.gxa.saas.portal.entity.vo.wylvo.OrderVo;
import com.gxa.saas.portal.entity.vo.wylvo.ShareVo;
import com.gxa.saas.portal.service.WGoodsService;
import com.gxa.saas.portal.service.WHomeService;
import com.gxa.saas.portal.service.WOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/worder")
public class OrderManagement {

    @Resource
    private WOrderService wOrderService;

    @Resource
    private WGoodsService wGoodsService;

    @ApiOperation("订单管理显示数据")
    @GetMapping("/orderData")
    public Map orderData(HttpServletRequest request){


        //根据登录账号获取商铺id
        String sid= JwtUtil.getUsername(request.getAttribute("token1").toString());
        Integer shopId = wGoodsService.selectShopId(sid);

        Integer newOrder = wOrderService.countNewOrder(shopId);

        List<OrderInfoVO> orderData = wOrderService.orderData(shopId);

        if(orderData.size()==0){
            List<OrderInfoVO> list1 = new ArrayList<>();
            OrderInfoVO goodsVo = new OrderInfoVO();
            list1.add(goodsVo);
            Map map = new HashMap();
            map.put("msg","暂无数据");
            map.put("code",200);
            map.put("data",list1);
            return map;
        }
        orderData.get(0).setNewOrdet(newOrder);

        Map map = new HashMap();
        map.put("msg","查询成功");
        map.put("code",200);
        map.put("data",orderData);
        return map;
    }

    @ApiOperation("根据状态查询")
    @GetMapping("/selectByStatus")
    public Map selectByStatus(HttpServletRequest request,@NotEmpty(message = "订单状态")Integer  status){

        System.out.println(status);

        //根据登录账号获取商铺id
        String sid= JwtUtil.getUsername(request.getAttribute("token1").toString());
        Integer shopId = wGoodsService.selectShopId(sid);

        List<OrderInfoVO> list = wOrderService.selectByStatus(shopId, status);
        if(list.size()==0){
            List<OrderInfoVO> list1 =  new ArrayList<>();
            Map map = new HashMap();
            map.put("msg","切换成功");
            map.put("code",200);
            map.put("data",list1);
            return map;
        }

        Map map = new HashMap();
        map.put("msg","切换成功");
        map.put("code",200);
        map.put("data",list);
        return map;
    }

    @ApiOperation("删除订单数据")
    @PostMapping("/delectOrder")
    public Map delectOrder(@NotEmpty(message = "订单id")Integer id){

        wOrderService.delectOrder(id);

        System.out.println(id);
        Map map = new HashMap();
        map.put("msg","删除成功");
        map.put("code",200);
        return map;
    }

    @ApiOperation("修改订单数据")
    @PostMapping("/updateOrder")
    public Map updateOrder( @RequestBody @Validated OrderVo order){


        wOrderService.updateOrder(order);

        System.out.println(order);
        Map map = new HashMap();
        map.put("msg","修改成功");
        map.put("code",200);

        return map;
    }
}
